# 避免ssl错误
# import ssl
# ssl._create_default_https_context = ssl._create_unverified_context
#
# import urllib.request as ur
#
# req = ur.Request(
#     url = 'http://www.baidu.com',
#     headers={
#         'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0'
#     }
# )
#
# content = ur.urlopen(req).read().decode('utf-8', 'ignore')
# print(content)

# 返回唯一的xpath结果
def xpath_one(contentx, path, default=None):
    rets = contentx.xpath(path)
    return rets[0] if rets else default


# 返回多个xpath结果
def xpath_all(contentx, path):
    rets = contentx.xpath(path)
    return rets


def xpath_union(contentx, path, split='', default=None):
    ret = split.join([ret.strip() for ret in contentx.xpath(path)])
    return ret if ret else default
